{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "629acf3e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>geohash_id</th>\n",
       "      <th>date_id</th>\n",
       "      <th>F_1</th>\n",
       "      <th>F_2</th>\n",
       "      <th>F_3</th>\n",
       "      <th>F_4</th>\n",
       "      <th>F_5</th>\n",
       "      <th>F_6</th>\n",
       "      <th>F_7</th>\n",
       "      <th>F_8</th>\n",
       "      <th>...</th>\n",
       "      <th>F_28</th>\n",
       "      <th>F_29</th>\n",
       "      <th>F_30</th>\n",
       "      <th>F_31</th>\n",
       "      <th>F_32</th>\n",
       "      <th>F_33</th>\n",
       "      <th>F_34</th>\n",
       "      <th>F_35</th>\n",
       "      <th>active_index</th>\n",
       "      <th>consume_index</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>4885e281g</td>\n",
       "      <td>20230104</td>\n",
       "      <td>-0.711</td>\n",
       "      <td>-0.696</td>\n",
       "      <td>-0.794</td>\n",
       "      <td>-0.727</td>\n",
       "      <td>-0.747</td>\n",
       "      <td>-0.792</td>\n",
       "      <td>1.539</td>\n",
       "      <td>2.433</td>\n",
       "      <td>...</td>\n",
       "      <td>0.073</td>\n",
       "      <td>0.344</td>\n",
       "      <td>0.006</td>\n",
       "      <td>-0.446</td>\n",
       "      <td>-0.502</td>\n",
       "      <td>-0.456</td>\n",
       "      <td>-0.457</td>\n",
       "      <td>-0.830</td>\n",
       "      <td>69.306</td>\n",
       "      <td>63.78</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>4885e281g</td>\n",
       "      <td>20230105</td>\n",
       "      <td>-0.909</td>\n",
       "      <td>-0.903</td>\n",
       "      <td>-0.947</td>\n",
       "      <td>-0.844</td>\n",
       "      <td>-0.856</td>\n",
       "      <td>-0.908</td>\n",
       "      <td>-0.371</td>\n",
       "      <td>0.990</td>\n",
       "      <td>...</td>\n",
       "      <td>0.055</td>\n",
       "      <td>0.298</td>\n",
       "      <td>0.007</td>\n",
       "      <td>-0.523</td>\n",
       "      <td>-0.558</td>\n",
       "      <td>-0.533</td>\n",
       "      <td>0.113</td>\n",
       "      <td>-0.887</td>\n",
       "      <td>68.881</td>\n",
       "      <td>61.62</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>4885e281g</td>\n",
       "      <td>20230106</td>\n",
       "      <td>-0.920</td>\n",
       "      <td>-0.925</td>\n",
       "      <td>-0.923</td>\n",
       "      <td>-0.852</td>\n",
       "      <td>-0.853</td>\n",
       "      <td>-0.915</td>\n",
       "      <td>-0.334</td>\n",
       "      <td>0.792</td>\n",
       "      <td>...</td>\n",
       "      <td>0.067</td>\n",
       "      <td>0.324</td>\n",
       "      <td>0.006</td>\n",
       "      <td>-0.535</td>\n",
       "      <td>-0.564</td>\n",
       "      <td>-0.540</td>\n",
       "      <td>0.367</td>\n",
       "      <td>-1.021</td>\n",
       "      <td>69.738</td>\n",
       "      <td>61.03</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>4885e281g</td>\n",
       "      <td>20230107</td>\n",
       "      <td>-0.926</td>\n",
       "      <td>-0.931</td>\n",
       "      <td>-0.943</td>\n",
       "      <td>-0.837</td>\n",
       "      <td>-0.850</td>\n",
       "      <td>-0.907</td>\n",
       "      <td>-0.993</td>\n",
       "      <td>-0.006</td>\n",
       "      <td>...</td>\n",
       "      <td>0.076</td>\n",
       "      <td>0.276</td>\n",
       "      <td>0.010</td>\n",
       "      <td>-0.534</td>\n",
       "      <td>-0.554</td>\n",
       "      <td>-0.521</td>\n",
       "      <td>0.550</td>\n",
       "      <td>-0.211</td>\n",
       "      <td>68.721</td>\n",
       "      <td>62.02</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>4885e281g</td>\n",
       "      <td>20230108</td>\n",
       "      <td>-0.750</td>\n",
       "      <td>-0.764</td>\n",
       "      <td>-0.818</td>\n",
       "      <td>-0.749</td>\n",
       "      <td>-0.764</td>\n",
       "      <td>-0.816</td>\n",
       "      <td>1.116</td>\n",
       "      <td>1.447</td>\n",
       "      <td>...</td>\n",
       "      <td>0.079</td>\n",
       "      <td>0.328</td>\n",
       "      <td>0.008</td>\n",
       "      <td>-0.468</td>\n",
       "      <td>-0.500</td>\n",
       "      <td>-0.419</td>\n",
       "      <td>-0.236</td>\n",
       "      <td>0.644</td>\n",
       "      <td>69.960</td>\n",
       "      <td>64.62</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5 rows × 39 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "  geohash_id   date_id    F_1    F_2    F_3    F_4    F_5    F_6    F_7  \\\n",
       "0  4885e281g  20230104 -0.711 -0.696 -0.794 -0.727 -0.747 -0.792  1.539   \n",
       "1  4885e281g  20230105 -0.909 -0.903 -0.947 -0.844 -0.856 -0.908 -0.371   \n",
       "2  4885e281g  20230106 -0.920 -0.925 -0.923 -0.852 -0.853 -0.915 -0.334   \n",
       "3  4885e281g  20230107 -0.926 -0.931 -0.943 -0.837 -0.850 -0.907 -0.993   \n",
       "4  4885e281g  20230108 -0.750 -0.764 -0.818 -0.749 -0.764 -0.816  1.116   \n",
       "\n",
       "     F_8  ...   F_28   F_29   F_30   F_31   F_32   F_33   F_34   F_35  \\\n",
       "0  2.433  ...  0.073  0.344  0.006 -0.446 -0.502 -0.456 -0.457 -0.830   \n",
       "1  0.990  ...  0.055  0.298  0.007 -0.523 -0.558 -0.533  0.113 -0.887   \n",
       "2  0.792  ...  0.067  0.324  0.006 -0.535 -0.564 -0.540  0.367 -1.021   \n",
       "3 -0.006  ...  0.076  0.276  0.010 -0.534 -0.554 -0.521  0.550 -0.211   \n",
       "4  1.447  ...  0.079  0.328  0.008 -0.468 -0.500 -0.419 -0.236  0.644   \n",
       "\n",
       "   active_index  consume_index  \n",
       "0        69.306          63.78  \n",
       "1        68.881          61.62  \n",
       "2        69.738          61.03  \n",
       "3        68.721          62.02  \n",
       "4        69.960          64.62  \n",
       "\n",
       "[5 rows x 39 columns]"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import numpy as np # 基础线性代数扩展包\n",
    "import pandas as pd # 数据处理工具箱\n",
    "df_bank = pd.read_csv(\"./train_90.csv\") # 读取文件\n",
    "df_bank.head() # 显示文件前5行"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "e43b88f0",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>F_1</th>\n",
       "      <th>F_2</th>\n",
       "      <th>F_3</th>\n",
       "      <th>F_4</th>\n",
       "      <th>F_5</th>\n",
       "      <th>F_6</th>\n",
       "      <th>F_7</th>\n",
       "      <th>F_8</th>\n",
       "      <th>F_9</th>\n",
       "      <th>F_10</th>\n",
       "      <th>...</th>\n",
       "      <th>F_26</th>\n",
       "      <th>F_27</th>\n",
       "      <th>F_28</th>\n",
       "      <th>F_29</th>\n",
       "      <th>F_30</th>\n",
       "      <th>F_31</th>\n",
       "      <th>F_32</th>\n",
       "      <th>F_33</th>\n",
       "      <th>F_34</th>\n",
       "      <th>F_35</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>-0.711</td>\n",
       "      <td>-0.696</td>\n",
       "      <td>-0.794</td>\n",
       "      <td>-0.727</td>\n",
       "      <td>-0.747</td>\n",
       "      <td>-0.792</td>\n",
       "      <td>1.539</td>\n",
       "      <td>2.433</td>\n",
       "      <td>-0.136</td>\n",
       "      <td>1.295</td>\n",
       "      <td>...</td>\n",
       "      <td>0.093</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.073</td>\n",
       "      <td>0.344</td>\n",
       "      <td>0.006</td>\n",
       "      <td>-0.446</td>\n",
       "      <td>-0.502</td>\n",
       "      <td>-0.456</td>\n",
       "      <td>-0.457</td>\n",
       "      <td>-0.830</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>-0.909</td>\n",
       "      <td>-0.903</td>\n",
       "      <td>-0.947</td>\n",
       "      <td>-0.844</td>\n",
       "      <td>-0.856</td>\n",
       "      <td>-0.908</td>\n",
       "      <td>-0.371</td>\n",
       "      <td>0.990</td>\n",
       "      <td>-0.935</td>\n",
       "      <td>0.479</td>\n",
       "      <td>...</td>\n",
       "      <td>0.098</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.055</td>\n",
       "      <td>0.298</td>\n",
       "      <td>0.007</td>\n",
       "      <td>-0.523</td>\n",
       "      <td>-0.558</td>\n",
       "      <td>-0.533</td>\n",
       "      <td>0.113</td>\n",
       "      <td>-0.887</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>-0.920</td>\n",
       "      <td>-0.925</td>\n",
       "      <td>-0.923</td>\n",
       "      <td>-0.852</td>\n",
       "      <td>-0.853</td>\n",
       "      <td>-0.915</td>\n",
       "      <td>-0.334</td>\n",
       "      <td>0.792</td>\n",
       "      <td>-0.532</td>\n",
       "      <td>0.334</td>\n",
       "      <td>...</td>\n",
       "      <td>0.086</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.067</td>\n",
       "      <td>0.324</td>\n",
       "      <td>0.006</td>\n",
       "      <td>-0.535</td>\n",
       "      <td>-0.564</td>\n",
       "      <td>-0.540</td>\n",
       "      <td>0.367</td>\n",
       "      <td>-1.021</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>-0.926</td>\n",
       "      <td>-0.931</td>\n",
       "      <td>-0.943</td>\n",
       "      <td>-0.837</td>\n",
       "      <td>-0.850</td>\n",
       "      <td>-0.907</td>\n",
       "      <td>-0.993</td>\n",
       "      <td>-0.006</td>\n",
       "      <td>-0.826</td>\n",
       "      <td>0.391</td>\n",
       "      <td>...</td>\n",
       "      <td>0.091</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.076</td>\n",
       "      <td>0.276</td>\n",
       "      <td>0.010</td>\n",
       "      <td>-0.534</td>\n",
       "      <td>-0.554</td>\n",
       "      <td>-0.521</td>\n",
       "      <td>0.550</td>\n",
       "      <td>-0.211</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>-0.750</td>\n",
       "      <td>-0.764</td>\n",
       "      <td>-0.818</td>\n",
       "      <td>-0.749</td>\n",
       "      <td>-0.764</td>\n",
       "      <td>-0.816</td>\n",
       "      <td>1.116</td>\n",
       "      <td>1.447</td>\n",
       "      <td>-0.547</td>\n",
       "      <td>0.939</td>\n",
       "      <td>...</td>\n",
       "      <td>0.099</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.079</td>\n",
       "      <td>0.328</td>\n",
       "      <td>0.008</td>\n",
       "      <td>-0.468</td>\n",
       "      <td>-0.500</td>\n",
       "      <td>-0.419</td>\n",
       "      <td>-0.236</td>\n",
       "      <td>0.644</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5 rows × 35 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "     F_1    F_2    F_3    F_4    F_5    F_6    F_7    F_8    F_9   F_10  ...  \\\n",
       "0 -0.711 -0.696 -0.794 -0.727 -0.747 -0.792  1.539  2.433 -0.136  1.295  ...   \n",
       "1 -0.909 -0.903 -0.947 -0.844 -0.856 -0.908 -0.371  0.990 -0.935  0.479  ...   \n",
       "2 -0.920 -0.925 -0.923 -0.852 -0.853 -0.915 -0.334  0.792 -0.532  0.334  ...   \n",
       "3 -0.926 -0.931 -0.943 -0.837 -0.850 -0.907 -0.993 -0.006 -0.826  0.391  ...   \n",
       "4 -0.750 -0.764 -0.818 -0.749 -0.764 -0.816  1.116  1.447 -0.547  0.939  ...   \n",
       "\n",
       "    F_26  F_27   F_28   F_29   F_30   F_31   F_32   F_33   F_34   F_35  \n",
       "0  0.093   0.0  0.073  0.344  0.006 -0.446 -0.502 -0.456 -0.457 -0.830  \n",
       "1  0.098   0.0  0.055  0.298  0.007 -0.523 -0.558 -0.533  0.113 -0.887  \n",
       "2  0.086   0.0  0.067  0.324  0.006 -0.535 -0.564 -0.540  0.367 -1.021  \n",
       "3  0.091   0.0  0.076  0.276  0.010 -0.534 -0.554 -0.521  0.550 -0.211  \n",
       "4  0.099   0.0  0.079  0.328  0.008 -0.468 -0.500 -0.419 -0.236  0.644  \n",
       "\n",
       "[5 rows x 35 columns]"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "X=df_bank.iloc[:,2:-2]\n",
    "y=df_bank['consume_index']\n",
    "X.head() #显示新的特征集"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "2da1f04f",
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.model_selection import train_test_split # 拆分数据集\n",
    "X_train, X_test, y_train, y_test = train_test_split(X, y, \n",
    "                                   test_size=0.2, random_state=0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "9f202459",
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.tree import DecisionTreeClassifier # 导入决策树分类器\n",
    "from sklearn.model_selection import GridSearchCV # 导入网格搜索工具\n",
    "from sklearn.ensemble import AdaBoostClassifier # 导入AdaBoost模型\n",
    "# from sklearn.metrics import (f1_score, confusion_matrix) # 导入评估标准\n",
    "from sklearn.metrics import mean_squared_error, r2_score\n",
    "\n",
    "dt = DecisionTreeClassifier() # 选择决策树分类器作为AdaBoost的基准算法\n",
    "ada = AdaBoostClassifier(dt) # AdaBoost模型"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "49894fe6",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Fitting 5 folds for each of 1120 candidates, totalling 5600 fits\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "c:\\Users\\JIN\\Anaconda3\\envs\\pytorch\\lib\\site-packages\\sklearn\\model_selection\\_validation.py:372: FitFailedWarning: \n",
      "5600 fits failed out of a total of 5600.\n",
      "The score on these train-test partitions for these parameters will be set to nan.\n",
      "If these failures are not expected, you can try to debug them by setting error_score='raise'.\n",
      "\n",
      "Below are more details about the failures:\n",
      "--------------------------------------------------------------------------------\n",
      "2800 fits failed with the following error:\n",
      "Traceback (most recent call last):\n",
      "  File \"c:\\Users\\JIN\\Anaconda3\\envs\\pytorch\\lib\\site-packages\\sklearn\\model_selection\\_validation.py\", line 680, in _fit_and_score\n",
      "    estimator.fit(X_train, y_train, **fit_params)\n",
      "  File \"c:\\Users\\JIN\\Anaconda3\\envs\\pytorch\\lib\\site-packages\\sklearn\\ensemble\\_weight_boosting.py\", line 486, in fit\n",
      "    return super().fit(X, y, sample_weight)\n",
      "  File \"c:\\Users\\JIN\\Anaconda3\\envs\\pytorch\\lib\\site-packages\\sklearn\\ensemble\\_weight_boosting.py\", line 146, in fit\n",
      "    iboost, X, y, sample_weight, random_state\n",
      "  File \"c:\\Users\\JIN\\Anaconda3\\envs\\pytorch\\lib\\site-packages\\sklearn\\ensemble\\_weight_boosting.py\", line 551, in _boost\n",
      "    return self._boost_discrete(iboost, X, y, sample_weight, random_state)\n",
      "  File \"c:\\Users\\JIN\\Anaconda3\\envs\\pytorch\\lib\\site-packages\\sklearn\\ensemble\\_weight_boosting.py\", line 616, in _boost_discrete\n",
      "    estimator.fit(X, y, sample_weight=sample_weight)\n",
      "  File \"c:\\Users\\JIN\\Anaconda3\\envs\\pytorch\\lib\\site-packages\\sklearn\\tree\\_classes.py\", line 942, in fit\n",
      "    X_idx_sorted=X_idx_sorted,\n",
      "  File \"c:\\Users\\JIN\\Anaconda3\\envs\\pytorch\\lib\\site-packages\\sklearn\\tree\\_classes.py\", line 203, in fit\n",
      "    check_classification_targets(y)\n",
      "  File \"c:\\Users\\JIN\\Anaconda3\\envs\\pytorch\\lib\\site-packages\\sklearn\\utils\\multiclass.py\", line 197, in check_classification_targets\n",
      "    raise ValueError(\"Unknown label type: %r\" % y_type)\n",
      "ValueError: Unknown label type: 'continuous'\n",
      "\n",
      "--------------------------------------------------------------------------------\n",
      "2800 fits failed with the following error:\n",
      "Traceback (most recent call last):\n",
      "  File \"c:\\Users\\JIN\\Anaconda3\\envs\\pytorch\\lib\\site-packages\\sklearn\\model_selection\\_validation.py\", line 680, in _fit_and_score\n",
      "    estimator.fit(X_train, y_train, **fit_params)\n",
      "  File \"c:\\Users\\JIN\\Anaconda3\\envs\\pytorch\\lib\\site-packages\\sklearn\\ensemble\\_weight_boosting.py\", line 486, in fit\n",
      "    return super().fit(X, y, sample_weight)\n",
      "  File \"c:\\Users\\JIN\\Anaconda3\\envs\\pytorch\\lib\\site-packages\\sklearn\\ensemble\\_weight_boosting.py\", line 146, in fit\n",
      "    iboost, X, y, sample_weight, random_state\n",
      "  File \"c:\\Users\\JIN\\Anaconda3\\envs\\pytorch\\lib\\site-packages\\sklearn\\ensemble\\_weight_boosting.py\", line 548, in _boost\n",
      "    return self._boost_real(iboost, X, y, sample_weight, random_state)\n",
      "  File \"c:\\Users\\JIN\\Anaconda3\\envs\\pytorch\\lib\\site-packages\\sklearn\\ensemble\\_weight_boosting.py\", line 557, in _boost_real\n",
      "    estimator.fit(X, y, sample_weight=sample_weight)\n",
      "  File \"c:\\Users\\JIN\\Anaconda3\\envs\\pytorch\\lib\\site-packages\\sklearn\\tree\\_classes.py\", line 942, in fit\n",
      "    X_idx_sorted=X_idx_sorted,\n",
      "  File \"c:\\Users\\JIN\\Anaconda3\\envs\\pytorch\\lib\\site-packages\\sklearn\\tree\\_classes.py\", line 203, in fit\n",
      "    check_classification_targets(y)\n",
      "  File \"c:\\Users\\JIN\\Anaconda3\\envs\\pytorch\\lib\\site-packages\\sklearn\\utils\\multiclass.py\", line 197, in check_classification_targets\n",
      "    raise ValueError(\"Unknown label type: %r\" % y_type)\n",
      "ValueError: Unknown label type: 'continuous'\n",
      "\n",
      "  warnings.warn(some_fits_failed_message, FitFailedWarning)\n",
      "c:\\Users\\JIN\\Anaconda3\\envs\\pytorch\\lib\\site-packages\\sklearn\\model_selection\\_search.py:972: UserWarning: One or more of the test scores are non-finite: [nan nan nan ... nan nan nan]\n",
      "  category=UserWarning,\n"
     ]
    },
    {
     "ename": "ValueError",
     "evalue": "Unknown label type: 'continuous'",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mValueError\u001b[0m                                Traceback (most recent call last)",
      "\u001b[1;32m~\\AppData\\Local\\Temp\\ipykernel_21864\\3369308371.py\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m      8\u001b[0m ada_gs = GridSearchCV(ada,param_grid = ada_param_grid, \n\u001b[0;32m      9\u001b[0m                         scoring=\"neg_mean_squared_error\", n_jobs= 1, verbose = 1)\n\u001b[1;32m---> 10\u001b[1;33m \u001b[0mada_gs\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mfit\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mX_train\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0my_train\u001b[0m\u001b[1;33m)\u001b[0m \u001b[1;31m# 拟合模型\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m     11\u001b[0m \u001b[0mada_gs\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mada_gs\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mbest_estimator_\u001b[0m \u001b[1;31m# 最佳模型\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     12\u001b[0m \u001b[0my_pred\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mada_gs\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mpredict\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mX_test\u001b[0m\u001b[1;33m)\u001b[0m \u001b[1;31m# 进行预测\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32mc:\\Users\\JIN\\Anaconda3\\envs\\pytorch\\lib\\site-packages\\sklearn\\model_selection\\_search.py\u001b[0m in \u001b[0;36mfit\u001b[1;34m(self, X, y, groups, **fit_params)\u001b[0m\n\u001b[0;32m    924\u001b[0m             \u001b[0mrefit_start_time\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mtime\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mtime\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    925\u001b[0m             \u001b[1;32mif\u001b[0m \u001b[0my\u001b[0m \u001b[1;32mis\u001b[0m \u001b[1;32mnot\u001b[0m \u001b[1;32mNone\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 926\u001b[1;33m                 \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mbest_estimator_\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mfit\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mX\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0my\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;33m**\u001b[0m\u001b[0mfit_params\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m    927\u001b[0m             \u001b[1;32melse\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    928\u001b[0m                 \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mbest_estimator_\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mfit\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mX\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;33m**\u001b[0m\u001b[0mfit_params\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32mc:\\Users\\JIN\\Anaconda3\\envs\\pytorch\\lib\\site-packages\\sklearn\\ensemble\\_weight_boosting.py\u001b[0m in \u001b[0;36mfit\u001b[1;34m(self, X, y, sample_weight)\u001b[0m\n\u001b[0;32m    484\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    485\u001b[0m         \u001b[1;31m# Fit\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 486\u001b[1;33m         \u001b[1;32mreturn\u001b[0m \u001b[0msuper\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mfit\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mX\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0my\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0msample_weight\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m    487\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    488\u001b[0m     \u001b[1;32mdef\u001b[0m \u001b[0m_validate_estimator\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32mc:\\Users\\JIN\\Anaconda3\\envs\\pytorch\\lib\\site-packages\\sklearn\\ensemble\\_weight_boosting.py\u001b[0m in \u001b[0;36mfit\u001b[1;34m(self, X, y, sample_weight)\u001b[0m\n\u001b[0;32m    144\u001b[0m             \u001b[1;31m# Boosting step\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    145\u001b[0m             sample_weight, estimator_weight, estimator_error = self._boost(\n\u001b[1;32m--> 146\u001b[1;33m                 \u001b[0miboost\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mX\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0my\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0msample_weight\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mrandom_state\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m    147\u001b[0m             )\n\u001b[0;32m    148\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32mc:\\Users\\JIN\\Anaconda3\\envs\\pytorch\\lib\\site-packages\\sklearn\\ensemble\\_weight_boosting.py\u001b[0m in \u001b[0;36m_boost\u001b[1;34m(self, iboost, X, y, sample_weight, random_state)\u001b[0m\n\u001b[0;32m    549\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    550\u001b[0m         \u001b[1;32melse\u001b[0m\u001b[1;33m:\u001b[0m  \u001b[1;31m# elif self.algorithm == \"SAMME\":\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 551\u001b[1;33m             \u001b[1;32mreturn\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_boost_discrete\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0miboost\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mX\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0my\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0msample_weight\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mrandom_state\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m    552\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    553\u001b[0m     \u001b[1;32mdef\u001b[0m \u001b[0m_boost_real\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0miboost\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mX\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0my\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0msample_weight\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mrandom_state\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32mc:\\Users\\JIN\\Anaconda3\\envs\\pytorch\\lib\\site-packages\\sklearn\\ensemble\\_weight_boosting.py\u001b[0m in \u001b[0;36m_boost_discrete\u001b[1;34m(self, iboost, X, y, sample_weight, random_state)\u001b[0m\n\u001b[0;32m    614\u001b[0m         \u001b[0mestimator\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_make_estimator\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mrandom_state\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mrandom_state\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    615\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 616\u001b[1;33m         \u001b[0mestimator\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mfit\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mX\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0my\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0msample_weight\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0msample_weight\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m    617\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    618\u001b[0m         \u001b[0my_predict\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mestimator\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mpredict\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mX\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32mc:\\Users\\JIN\\Anaconda3\\envs\\pytorch\\lib\\site-packages\\sklearn\\tree\\_classes.py\u001b[0m in \u001b[0;36mfit\u001b[1;34m(self, X, y, sample_weight, check_input, X_idx_sorted)\u001b[0m\n\u001b[0;32m    940\u001b[0m             \u001b[0msample_weight\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0msample_weight\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    941\u001b[0m             \u001b[0mcheck_input\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mcheck_input\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 942\u001b[1;33m             \u001b[0mX_idx_sorted\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mX_idx_sorted\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m    943\u001b[0m         )\n\u001b[0;32m    944\u001b[0m         \u001b[1;32mreturn\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32mc:\\Users\\JIN\\Anaconda3\\envs\\pytorch\\lib\\site-packages\\sklearn\\tree\\_classes.py\u001b[0m in \u001b[0;36mfit\u001b[1;34m(self, X, y, sample_weight, check_input, X_idx_sorted)\u001b[0m\n\u001b[0;32m    201\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    202\u001b[0m         \u001b[1;32mif\u001b[0m \u001b[0mis_classification\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 203\u001b[1;33m             \u001b[0mcheck_classification_targets\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0my\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m    204\u001b[0m             \u001b[0my\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mnp\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mcopy\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0my\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    205\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32mc:\\Users\\JIN\\Anaconda3\\envs\\pytorch\\lib\\site-packages\\sklearn\\utils\\multiclass.py\u001b[0m in \u001b[0;36mcheck_classification_targets\u001b[1;34m(y)\u001b[0m\n\u001b[0;32m    195\u001b[0m         \u001b[1;34m\"multilabel-sequences\"\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    196\u001b[0m     ]:\n\u001b[1;32m--> 197\u001b[1;33m         \u001b[1;32mraise\u001b[0m \u001b[0mValueError\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"Unknown label type: %r\"\u001b[0m \u001b[1;33m%\u001b[0m \u001b[0my_type\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m    198\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    199\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mValueError\u001b[0m: Unknown label type: 'continuous'"
     ]
    }
   ],
   "source": [
    "# 使用网格搜索优化参数\n",
    "ada_param_grid = {\"base_estimator__criterion\" : [\"gini\", \"entropy\"],\n",
    "                  \"base_estimator__splitter\" :   [\"best\", \"random\"],\n",
    "                  \"base_estimator__random_state\" :   [7,9,10,12,15],\n",
    "                  \"algorithm\" : [\"SAMME\",\"SAMME.R\"],\n",
    "                  \"n_estimators\" :[1,2,5,10],\n",
    "                  \"learning_rate\":  [0.0001, 0.001, 0.01, 0.1, 0.2, 0.3,1.5]}\n",
    "ada_gs = GridSearchCV(ada,param_grid = ada_param_grid, \n",
    "                        scoring=\"neg_mean_squared_error\", n_jobs= 1, verbose = 1)\n",
    "ada_gs.fit(X_train,y_train) # 拟合模型\n",
    "ada_gs = ada_gs.best_estimator_ # 最佳模型\n",
    "y_pred = ada_gs.predict(X_test) # 进行预测"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a89ca550",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 计算MSE和R-squared\n",
    "mse = mean_squared_error(y_test, y_pred)\n",
    "r2 = r2_score(y_test, y_pred)\n",
    "\n",
    "# 输出模型评估结果和目标方程\n",
    "print('MSE:', mse)\n",
    "print('R-squared:', r2)\n",
    "\n",
    "# print(\"Adaboost测试准确率: {:.2f}%\".format(ada_gs.score(X_test, y_test)*100))\n",
    "# print(\"Adaboost测试F1分数: {:.2f}%\".format(f1_score(y_test, y_pred)*100))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python [conda env:PyTorch]",
   "language": "python",
   "name": "conda-env-PyTorch-py"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.16"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
